/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pe.edu.upc.dew.cobranzas.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import pe.edu.upc.dew.cobranzas.model.Banco;

/**
 *
 * @author Ivan
 */
public class BancoDaoImpl implements BancoDao {

     private List<Banco> bancos;

    public BancoDaoImpl() {
        this.bancos = new ArrayList<Banco>();
        this.cargarBancos();
    }

    public void cargarBancos() {
          Connection connection = null;
        Banco banco = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            connection = ConnectionUtils.getConnection();
            st = connection.createStatement();
            rs = st.executeQuery("select * from T_Banco");
            rs.next();
            do {

                banco = new Banco();
                banco.setIdBanco(rs.getInt("idBanco"));
                banco.setDescripcion(rs.getString("descripcion"));
                
                this.bancos.add(banco);

            } while (rs.next());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ConnectionUtils.closeResultSet(rs);
            ConnectionUtils.closeStatement(st);
            ConnectionUtils.closeConnection(connection);
        }
    }

    public List<Banco> Listado() {
        return this.bancos;
    }

     public Banco getBancoxId(int idBanco) {
        Banco banco = new Banco();
        for (int i = 1; i <= this.bancos.size(); i++) {
            if (this.bancos.get(i - 1).getIdBanco().equals(idBanco)) {
                banco = this.bancos.get(i - 1);
                break;
            }
        }

        return banco;
    }

}
